package com.flux.ui.widget

import android.content.Context
import android.os.Handler
import android.os.Message
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.view.MotionEvent

import java.lang.ref.WeakReference

/**
 * Created by hsy on 2016/4/11.
 */
class DPAutoScrollViewPager : ViewPager {

    private val mOnPageChangeListener = object : ViewPager.OnPageChangeListener {
        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

        }

        override fun onPageSelected(position: Int) {
            if (null != mOnAutoScrollListener) {
                mOnAutoScrollListener!!.onAutoScrollFinish(currentItem)
            }
        }

        override fun onPageScrollStateChanged(state: Int) {

        }
    }

    private var mOnAutoScrollListener: OnAutoScrollListener? = null
    private var mDuration: Long = 5000
    private val handler = ViewHandler(this)

    interface OnAutoScrollListener {
        fun onAutoScrollStart(positionBefore: Int)

        fun onAutoScrollFinish(positionAfter: Int)
    }

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    private fun init() {
        addOnPageChangeListener(mOnPageChangeListener)
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        val act = MotionEvent.ACTION_MASK and ev.action
        if (MotionEvent.ACTION_DOWN == act) {
            handler.removeMessages(0)
        }
        if (MotionEvent.ACTION_UP == act || MotionEvent.ACTION_CANCEL == act) {
            startScroll(mDuration, mOnAutoScrollListener)
        }
        return super.dispatchTouchEvent(ev)
    }

    fun startScroll(duration: Long, listener: OnAutoScrollListener?) {
        this.mOnAutoScrollListener = listener
        this.mDuration = duration

        currentItem = currentItem
        handler.removeMessages(0)
        handler.sendEmptyMessageDelayed(0, mDuration)
    }

    fun stopScroll() {
        handler.removeMessages(0)
    }

    private class ViewHandler internal constructor(viewPager: DPAutoScrollViewPager) : Handler() {
        private val mViewPager: WeakReference<DPAutoScrollViewPager> = WeakReference(viewPager)
        private var current = 0

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            val viewPager = mViewPager.get() ?: return
            if (null != viewPager.mOnAutoScrollListener) {
                viewPager.mOnAutoScrollListener!!.onAutoScrollStart(viewPager.currentItem)
            }
            current++
            if (viewPager.adapter?.count == current) {
                current = 0
                viewPager.currentItem = current
                viewPager.setCurrentItem(viewPager.currentItem, false)
                sendEmptyMessageDelayed(0, viewPager.mDuration)
            } else {
                viewPager.currentItem = current
                sendEmptyMessageDelayed(0, viewPager.mDuration)
            }
        }
    }
}